<template>
  <div id="app">
  	<e-main  @showDialog="openCabinet" :isConfirm="confirmStatus" :message="btnMessage" />
  	<e-dialog v-model="dialogStatus"  :message="confirmMessage" @onConfirm="onChangeRocover"/>
  	<div class="loading-dialog" v-show="loading">
	      <img src="./assets/images/loading-spinning-bubbles.svg" alt="">
	    </div>
    <e-dialog v-model="errDialog"  :message="errMessage" @onConfirm="errDialog=false"/>
  </div>
</template>
<script>
  import EMain from './components/Main.vue'
  import EDialog from './components/Dialog.vue'
  export default {
    components:{EMain,EDialog},
    data(){
      return{
      		dialogStatus:false,
      		confirmStatus:false,
      		loading:false,
          errDialog:false,
      		confirmMessage:'请把快递盒放进指定的回收柜关门后请确认!',
          errMessage:'',
          code:this.$route.query.code,
          openId:'',
          cabinetId:this.$route.query.cabinetId || 110

      }
    },
    computed:{
    	btnMessage(){
    		return this.confirmStatus ? '回收完成' : '我要回收'
    	}
    },
    methods:{
    	onChangeRocover(){
        this.dialogStatus = false
        this.confirmStatus = true
        // this.loading = true
        // this.$axios.post('/closeCabinet',{
        //   cabinetId:this.cabinetId,
        //   openId:this.openId
        //
        // }).then(res=>{
        //   this.dialogStatus = false
        //   this.confirmStatus = true
        // }).catch(err=>{
        //   this.errDialog = true
        //   this.errMessage = '回收失败，请关门重扫二维码！'
        // }).finally(()=>{
        //   this.loading = false
        // })
    	},
      openCabinet(){
    	  if(this.confirmStatus){
    	    return
        }else{
    	    this.loading = true
    	    this.$axios.post('/openCabinet',{
            cabinetId:this.cabinetId,
            openId:this.openId

          }).then(res=>{
            this.dialogStatus = true
          }).catch(err=>{
            console.log(err)
            this.errDialog = true
            this.errMessage = '打开失败，请刷新重试！'
          }).finally(()=>{
            this.loading = false
          })
        }
      },
      getopenId(){
    	  this.$axios.post('/getOpenId',{code:this.code}).then(res=>{
    	    this.openId = res.data.openId || '123'
        }).catch(err=>{
          this.errDialog = true
          this.errMessage = '获取openId失败！'
        })
      }
    },
    mounted(){
      this.getopenId()
    }
  }
</script>
<style>
#app{background-color: #fff;}
</style>
